﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Areas.Mobile.Lib;

namespace Web.Areas.Mobile.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                //filterContext.Exception = null;
                filterContext.ExceptionHandled = true;
                filterContext.Result = Json(new
                {
                    status = false,
                    errmsg = filterContext.Exception.Message,
                }, JsonRequestBehavior.AllowGet);
            }

            var e = filterContext.Exception;
            if (!filterContext.ExceptionHandled)
            {
                //filterContext.Exception = null;
                filterContext.Result = Redirect("/mobile/survey/notopen?msg=" + filterContext.Exception.Message + "&uid=" + MobileAccount.ID + "&qs=" + filterContext.RequestContext.HttpContext.Request.QueryString);
                filterContext.ExceptionHandled = true;
            }
            base.OnException(filterContext);
        }

        /// <summary>
        /// 返回错误消息
        /// </summary>
        /// <param name="errmsg"></param>        
        /// <returns></returns>
        protected JsonResult ErrorMsg(string errmsg)
        {
            return this.ErrorMsg<string>(errmsg, null);
        }

        /// <summary>
        /// 返回错误消息
        /// </summary>
        /// <param name="errmsg"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected JsonResult ErrorMsg<T>(string errmsg, T data)
        {
            return Json(new
            {
                status = false,
                errmsg = errmsg,
                data = data
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>                
        /// <returns></returns>
        protected JsonResult SuccessMsg()
        {
            return this.SuccessMsg<string>(null);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>        
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        protected JsonResult SuccessMsg<T>(string errmsg, T data)
        {
            return Json(new
            {
                status = true,
                errmsg = errmsg,
                data = data
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 返回成功消息
        /// </summary>        
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        protected JsonResult SuccessMsg<T>(T data)
        {
            return Json(new
            {
                status = true,
                errmsg = "ok",
                data = data
            }, JsonRequestBehavior.AllowGet);
        }
    }
}
